<template>
  <div class="message" :class="{ minimized: isMinimized }">
    <el-card class="message-card">
      <template #header>
        <div class="card-header">
          <span>消息通知</span>
          <div class="header-actions">
            <el-tooltip :content="isMinimized ? '展开' : '最小化'">
              <i 
                :class="isMinimized ? 'el-icon-plus' : 'el-icon-minus'" 
                @click="toggleMinimize"
              ></i>
            </el-tooltip>
          </div>
        </div>
      </template>
      <div class="message-body">
        <div v-if="unreadCount > 0" class="new-message-indicator">
          您有 {{ unreadCount }} 条新消息
          <el-button type="text" @click="viewNewMessages">查看</el-button>
        </div>
        <div class="message-history" v-show="!isMinimized">
          <div v-for="(msg, index) in displayedHistory" :key="index" class="message-item">
            <span>{{ msg }}</span>
          </div>
        </div>
      </div>
    </el-card>
    <!-- 最小化状态显示 -->
    <div v-if="isMinimized" class="minimized-view" @click="restoreWindow">
      <i class="el-icon-message"></i>
      <el-badge :value="unreadCount" class="badge" v-show="unreadCount > 0"></el-badge>
    </div>
  </div>
</template>

<script>
import websocketClient from '@/utils/websocketClient';
import config from '@/config/index.js';

export default {
  name: 'MessageNotification',
  data() {
    return {
      history: [],
      isMinimized: false,      // 是否最小化状态
      unreadCount: 0,         // 未读消息计数
      minMessages: 2          // 最小化时显示的消息数量
    };
  },
  computed: {
    // 根据状态显示不同数量的消息历史
    displayedHistory() {
      if (this.isMinimized) {
        return this.history.slice(-this.minMessages);
      }
      return this.history;
    }
  },
  created() {
    websocketClient.connect(config.websocketUrl);
    websocketClient.onMessage(this.handleMessage);
  },
  beforeDestroy() {
    websocketClient.close();
  },
  methods: {
    sendNotification() {
      websocketClient.send(JSON.stringify({
        type: 'notification',
        content: '系统更新通知'
      }));
    },
    handleMessage(data) {
      // 收到新消息时添加到历史记录
      this.history.push(data);
      
      // 如果是最小化状态，增加未读计数
      if (this.isMinimized) {
        this.unreadCount++;
      } else if (this.unreadCount > 0) {
        // 如果当前是展开状态但之前有未读，重置计数器
        this.unreadCount = 0;
      }
      
      // 可选：自动恢复窗口显示
      if (this.isMinimized) {
        // 如果是重要通知类型，可以自动恢复
        if (data.type && data.type === 'urgent') {
          this.restoreWindow();
        }
      }
    },
    // 切换最小化状态
    toggleMinimize() {
      this.isMinimized = !this.isMinimized;
      // 展开时重置未读计数
      if (!this.isMinimized) {
        this.unreadCount = 0;
      }
    },
    // 点击最小化视图恢复窗口
    restoreWindow() {
      this.isMinimized = false;
      this.unreadCount = 0;
    },
    // 查看新消息
    viewNewMessages() {
      this.restoreWindow();
      // 可以滚动到最新消息
      this.$nextTick(() => {
        const container = this.$el.querySelector('.message-history');
        if (container) {
          container.scrollTop = container.scrollHeight;
        }
      });
    },
   
  }
};
</script>

<style scoped>
.message {
  width: 300px;
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 9999;
  transition: transform 0.3s ease;
}

.message.minimized {
  transform: translateY(calc(100% - 40px));
}

.message-card {
  height: 250px;
  display: flex;
  flex-direction: column;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  font-weight: bold;
  background-color: #f5f7fa;
  border-bottom: 1px solid #ebeef5;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.header-actions i {
  cursor: pointer;
  font-size: 16px;
  color: #606266;
  transition: color 0.3s;
}

.header-actions i:hover {
  color: #409EFF;
}

.message-body {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.new-message-indicator {
  padding: 8px 15px;
  background-color: #f0f7ff;
  color: #409EFF;
  font-size: 12px;
  border-bottom: 1px solid #ebeef5;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.message-history {
  flex: 1;
  overflow-y: auto;
  padding: 10px 15px;
  font-size: 14px;
}

.message-item {
  padding: 6px 0;
  border-bottom: 1px dashed #ebeef5;
}

.message-item:last-child {
  border-bottom: none;
}

.minimized-view {
  position: absolute;
  top: -35px;
  left: 0;
  right: 0;
  height: 40px;
  background: #f5f7fa;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
  border: 1px solid #ebeef5;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0 15px;
  box-shadow: 0 -1px 4px rgba(0, 0, 0, 0.1);
}

.minimized-view i {
  font-size: 18px;
  color: #909399;
}

.badge {
  position: absolute;
  top: -8px;
  right: 5px;
}
</style>